package com.nbx.app.neheport.render;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.nbx.app.neheport.R;
import com.nbx.library.glutes.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLU;
import android.opengl.GLUtils;


public class Render15 extends RenderBase {
    
    private float rotx = 0.0f;
    private float roty = 0.0f;
    
    private float speed = 2.0f;
    
    private int mTextureID[];
 
    
    float LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
    float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
    float LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };
    
	private float mWidth;
	private float mHeight;
	   
    private FloatBuffer mAmbientBuffer;
    private FloatBuffer mDiffuseBuffer;
    private FloatBuffer mPositionBuffer;
    
    //font
    private FloatBuffer mVetexBuffer;
    private FloatBuffer mTexBuffer;
   
    public Render15(Context context) {
        super(context);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {       
        mAmbientBuffer = util.createFloatBuffer(LightAmbient);
        mDiffuseBuffer = util.createFloatBuffer(LightDiffuse);
        mPositionBuffer = util.createFloatBuffer(LightPosition);    
        
        //font
        mVetexBuffer = ByteBuffer.allocateDirect(12 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    	mTexBuffer = ByteBuffer.allocateDirect(8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 
        
        loadBitmapTex(gl);

        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, mAmbientBuffer);	
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, mDiffuseBuffer);	
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION,mPositionBuffer);
        gl.glEnable(GL10.GL_LIGHT1);
        
        gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
        gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
        gl.glClearDepthf(1.0f); // Depth Buffer Setup
        gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // Really Nice Perspective Calculations
    }
    
    public void onDrawFrame(GL10 gl) {        
        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
        gl.glLoadIdentity();

        gl.glEnable(GL10.GL_BLEND);	
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
    	
    	gl.glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
    	glDrawString(gl,
    			(mWidth - 224)/2,
    			(mHeight-20)/2,
    			"Not supported!");	
    	gl.glDisable(GL10.GL_BLEND);
        
        rotx += (speed + 0.1f);
        roty += speed;
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
    	mWidth = width;
    	mHeight = height;
    	
        gl.glViewport(0,0,width,height);                       // Reset The Current Viewport

        gl.glMatrixMode(GL10.GL_PROJECTION);                        // Select The Projection Matrix
        gl.glLoadIdentity();                                   // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f,(float)width/(float)height,0.1f,100.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);                         // Select The Modelview Matrix
        gl.glLoadIdentity();   
    }
    
    private void loadBitmapTex(GL10 gl){
        mTextureID = new int[1];
        gl.glGenTextures(1, mTextureID, 0);

        Bitmap bmp = util.getTextureFromBitmapResource(mContext, R.drawable.font);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);


        bmp.recycle();
        
        return;
    }
 
    
    private void glDrawString(GL10 gl, float x, float y, String t) {

		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]);
		gl.glDisable(GL10.GL_DEPTH_TEST);

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();

		gl.glLoadIdentity();
		gl.glOrthof(0, mWidth, 0, mHeight, -1, 1);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glLoadIdentity();

		gl.glTranslatef(x, y, 0);
		
		float totalX = 0.0f;

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        
		for(int i=0; i<t.length(); i++){
			mVetexBuffer.put(0, totalX);
			mVetexBuffer.put(1, 0.0f);
			mVetexBuffer.put(2, 0.0f);
			
			mVetexBuffer.put(3, totalX + 16.0f);
			mVetexBuffer.put(4, 0.0f);
			mVetexBuffer.put(5, 0.0f);
			
			mVetexBuffer.put(6, totalX);
			mVetexBuffer.put(7, 16.0f);
			mVetexBuffer.put(8, 0.0f);
			
			mVetexBuffer.put(9, totalX + 16.0f);
			mVetexBuffer.put(10, 16.0f);
			mVetexBuffer.put(11, 0.0f);

			int c = t.charAt(i)- ' ';
			float cx = (float) (c % 16) / 16.0f;
			float cy = (float) (c / 16) / 16.0f;

			mTexBuffer.put(0, cx);
			mTexBuffer.put(1, 1.0f - cy - 0.0625f);

			mTexBuffer.put(2, cx + 0.0625f);
			mTexBuffer.put(3, 1.0f - cy - 0.0625f);

			mTexBuffer.put(4, cx);
			mTexBuffer.put(5, 1.0f - cy);

			mTexBuffer.put(6, cx + 0.0625f);
			mTexBuffer.put(7, 1.0f - cy);
			
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVetexBuffer);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
			
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);	
			
			totalX += 16.0f;
		}

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPopMatrix();
		gl.glEnable(GL10.GL_DEPTH_TEST);
		return;
	}
}
